<?php

namespace Modules\ShopAdmin\Models;
class ShopOrderInfo extends BaseApiModel
{ 
	/**
	 * @name  关联商品
	 */
	 public function product_to()
    {
        return $this->belongsTo('Modules\ShopAdmin\Models\ShopProduct','product_id','id');
    }
	/**
	 * @name  关联订单
	 */
	 public function order_to()
    {
        return $this->belongsTo('Modules\ShopAdmin\Models\ShopOrder','order_id','id');
    }
	/**
	 * @name  已发货订单列表
	 * @param  data array 搜索数据
	 * @return  JSON
	 */
	public function shipped_list_data($data){
		$model = $this->query();
		$list = $model->select('id','order_id','product_id','specs','num','money','postage')
				->whereHas('order_to',function($query)use ($data){
					$query->where('status',3);
					if (!empty($data['user_phone'])){
						$query->where('user_phone','like','%' . $data['user_phone'] . '%');	
					} 
					if (!empty($data['order_no'])){
						$query->where('order_no','like','%' . $data['order_no'] . '%');	
					} 
					if (!empty($data['delivery_time'])){
						$query->whereBetween('delivery_time',explode('|',$data['delivery_time']));
					}
					if(!empty($data['user_name'])){
						$query->where('user_name', 'like', '%' . $data['user_name'] . '%');
					}
				})
				->whereHas('product_to',function($query)use ($data){
					if(!empty($data['product_name'])){
						$query->where('name', 'like', '%' . $data['product_name'] . '%');
					}
				})
				->with([
					'product_to'=>function($query){
						$query->select('id','name');
					},
					'order_to'=>function($query){
						$query->select('id','order_no','user_name','user_address','user_phone','delivery_time','comment');
					}
				])
				->orderBy('id','desc')
				->paginate($data['pagesize'])
				->toArray();			
		return $this->api_success(['data'=>[
			'list'=>$list['data'],
			'total'=>$list['total']
		]]);
	}
}
